package com.stone.oauth.client;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.oltu.oauth2.client.HttpClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.OAuthClientResponse;
import org.apache.oltu.oauth2.client.response.OAuthClientResponseFactory;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.utils.OAuthUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Created with IntelliJ IDEA. </p>
 * <p>User: Stony </p>
 * <p>Date: 2016/7/12 </p>
 * <p>Time: 13:13 </p>
 * <p>Version: 1.0 </p>
 */
public class HttpClient4 implements HttpClient {

    private static org.apache.http.client.HttpClient httpClient = new DefaultHttpClient();


    @Override
    public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers, String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
        InputStream responseBody = null;
        String contentType = null;
        int responseCode = 500;
        Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
        try {
            HttpResponse response;
            if (OAuthUtils.isEmpty(requestMethod) || "GET".equalsIgnoreCase(requestMethod)) {
                HttpUriRequest get = new HttpGet(request.getLocationUri());
                setHeader(get, headers);
                response = httpClient.execute(get);
            }else{
                HttpUriRequest post = new HttpPost(request.getLocationUri());
                setHeader(post, headers);
                post.setParams(new BasicHttpParams());
                response = httpClient.execute(post);
            }
            HttpEntity httpEntity = response.getEntity();
            contentType = httpEntity.getContentType().getValue();
            responseCode = response.getStatusLine().getStatusCode();
            responseBody = httpEntity.getContent();
            Header[] repHeaders = response.getAllHeaders();
            fillHeader(repHeaders, responseHeaders);
//            EntityUtils.consume(httpEntity);
        } catch (IOException e) {
            throw new OAuthSystemException(e);
        }
        return OAuthClientResponseFactory.createCustomResponse(responseBody, contentType, responseCode, responseHeaders, responseClass);
    }
    public void setHeader(HttpUriRequest request, Map<String, String> headers){
        for(Map.Entry<String, String> header : headers.entrySet()){
            request.setHeader(header.getKey(), header.getValue());
        }
    }
    public void fillHeader(Header[] headers, Map<String, List<String>> responseHeaders){
        for(Header header : headers){
            List<String> values = responseHeaders.get(header.getName());
            if(null == values){
                values = new ArrayList<>();
                values.add(header.getValue());
                responseHeaders.put(header.getName(), values);
            }else{
                values.add(header.getValue());
            }
        }
    }
    @Override
    public void shutdown() {

    }
}
